<?php
class Goods_base_model extends CI_Model
{
    private $table = 'goods_base';
    private $table_2 = 'goods_map_profit';
    
    public function findByGoodsId($goods_id)
    {
        $this->db->where('goods_id', $goods_id);
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('bit_opt & 1=', 1); //上架
        $this->db->where('click_count !=', 1, false); //1:前台隐藏
        return $this->db->get($this->table);
    }
    
    public function findByParams($params=array())
    {
        if (!empty($params['sid'])) {
            $this->db->where('sid', $params['sid']);
        }
        if (!empty($params['sids'])) {
            $this->db->where_in('sid', $params['sids']);
        }
        if (!empty($params['goods_ids'])) {
            $this->db->where_in('goods_id', $params['goods_ids']);
        }
        if (!empty($params['is_scan'])) {
            $this->db->where('goods_base.is_scan', $params['is_scan']); //1:网络预订 2:扫码订票
        }
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('bit_opt & 1=', 1); //上架
        $this->db->where('click_count !=', 1, false); //1:前台隐藏
        $this->db->where('sell_end_time >=', strtotime(date('Y-m-d H:i:s')));//有效期内
        if (!empty($params['orderby'])) {
            $this->db->order_by($params['orderby']);
        }
        return $this->db->get($this->table);
    }
    
    public function findByUidParams($params=array())
    {
        $this->db->select('goods_base.*');
        $this->db->from($this->table.' AS goods_base');
        $this->db->join($this->table_2.' AS goods_map_profit', 'goods_map_profit.goods_id=goods_base.goods_id', 'INNER');
        if (!empty($params['uid'])) {
            $this->db->where('goods_map_profit.uid', $params['uid']);
        }

        if (!empty($params['is_scan'])) { //是否扫码购票
            $this->db->where('goods_base.is_scan', $params['is_scan']);
        } else {
            $this->db->where('goods_base.is_scan', 1);
        }
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('bit_opt & 1=', 1); //上架
        $this->db->where('click_count !=', 1, false); //1:前台隐藏
        $this->db->where('sell_end_time >=', strtotime(date('Y-m-d H:i:s')));//有效期内
        if (!empty($params['orderby'])) {
            $this->db->order_by($params['orderby']);
        }
        return $this->db->get();
    }
    
    public function orderTotal($params=array())
    {
        $this->db->select('goods_id,shop_price,off_bonus, market_price, sfz_required');
        $this->db->from($this->table);
        if (!empty($params['goods_ids'])) {
            $this->db->where_in('goods_id', $params['goods_ids']);
        }
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('bit_opt & 1=', 1); //上架
        $this->db->where('click_count !=', 1, false); //1:前台隐藏
        return $this->db->get();
    }

    /**
     * 门票修改 变更库存或销量
     * @param unknown $param
     */
    public function setTicketStock($params=array())
    {
        if (!empty($params['limit_number'])) {//可售数量
            $this->db->set('limit_number', 'limit_number+' . $params['limit_number'], FALSE);
        }
        if (!empty($params['sold_number'])) {//已售数量
            $this->db->set('sold_number', 'sold_number+' . $params['sold_number'], FALSE);
        }
        $this->db->where('goods_id', $params['goods_id']);
        return $this->db->update($this->table);
    }
    
    /**
     * 获取所有门票
     * @param 景区编号 $sid 
     */
    public function findBySid($sid)
    {
        $this->db->where('sid', $sid);
        $this->db->order_by('cat_id', 'ASC');
        return $this->db->get($this->table);
    }
    
    /**
     * 获取所有门票
     * @param 景区编号 $sid
     */
    public function findGoodsBaseBySid($sid)
    {
        $this->db->where('sid', $sid);
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('(bit_opt&1=1)'); //已上架
        $this->db->where_in('cat_id', array(1, 2, 8, 16));
        $this->db->order_by('cat_id', 'ASC');
        return $this->db->get($this->table);
    }
    
    /**
     * 更新goods_base 的表 可售数量 和 已售数量
     * @param unknown $goods_id
     * @param unknown $goods_num
     */
    public function updateLimitSoldNum($goods_id,$goods_num)
    {
    	$this->db->set('limit_number',"limit_number - $goods_num",FALSE);
	    $this->db->set('sold_number',"sold_number + $goods_num",FALSE);
	    $this->db->where('goods_id',$goods_id);
	    $this->db->update($this->table);
    }
    
    /**
     * 根据goods_id 获取所有东西--要什么取什么
     * @param unknown $goods_id
     */
    public function getResultByGoodsId($goods_id)
    {
        $this->db->select('o_goods_id,o_api_id');
        $this->db->from($this->table);
        $this->db->where('goods_id',$goods_id);
        return $this->db->get();
    }

    /**
     * 查找该景区的最小价格
     */
    public function findMinPrice($sid)
    {
        $this->db->select_min('shop_price', 'min_price');
        $this->db->from($this->table);
        $this->db->where('sid',$sid);
        $this->db->where('is_check', 2); //审核通过
        $this->db->where('(bit_opt&1=1)'); //已上架
        $this->db->where('is_scan', 1); //网络预订
        return $this->db->get();
    }
}